package com.joker.userExam.service.impl;

import com.joker.userExam.dao.IStudentScore;
import com.joker.userExam.dao.ISubjectOneQuestion;
import com.joker.userExam.dao.repo.StudentScoreRepo;
import com.joker.userExam.model.StudentScore;
import com.joker.userExam.model.SubjectOneQuestion;
import com.joker.userExam.service.IExamQuestionsSubjectOne;
import com.joker.userExam.dao.repo.SubjectOneQuestionRepo;
import com.joker.userExam.service.IUserLogin;
import com.joker.userExam.utils.ResultUtil;
import com.joker.userExam.utils.enumUtils.ExamType;
import com.joker.userExam.utils.enumUtils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.*;

/**
 * @author 李杰
 * @date 2021/3/21 - 18:39
 */
@Service
public class ExamQuestionsSubjectOneImpl implements IExamQuestionsSubjectOne {
    @Resource
    SubjectOneQuestionRepo subjectOneQuestionRepo;
    @Autowired
    ISubjectOneQuestion subjectOneQuestion;
    @Autowired
    StudentScoreRepo studentScoreRepo;

    @Override
    public SubjectOneQuestion getSubjectOneCarById(String id) {

        return subjectOneQuestionRepo.getById(id);
    }

    @Override
    public ResultUtil subjectOneExamByType(Integer examType) {
        //随机出题结果
        List<SubjectOneQuestion> result = new ArrayList<>();
        //根据科目一考试类型进行不同的查询
        List<Integer> type = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 7));
        if (examType == ExamType.TRUNK.ordinal()) {
            type.add(5);
        }
        if (examType == ExamType.BUS.ordinal()) {
            type.add(6);
        }
        List<SubjectOneQuestion> subjectOneQuestions = subjectOneQuestion.queryAllByType(type);

        //根据结果数量产生100个随机数
        List<Integer> randomList = RandomUtils.getRandomList(subjectOneQuestions.size() - 1, 100);
        for (Integer index : randomList) {
            result.add(subjectOneQuestions.get(index));
        }
        return ResultUtil.ok("success", result);
    }

    @Override
    public ResultUtil examResSubmit(StudentScore examRes) {
        boolean save = studentScoreRepo.save(examRes);
        if (save) {
            return ResultUtil.ok("success");
        } else {
            return ResultUtil.error("error");
        }
    }

}
